<?php
/**
* @package controller
* @file indexController.php
* @date 15.09.2009
* @version 1.0
* @copyright sample
* @author Harinath Reddy
* @todo phpDoc comments
*/
require_once 'Zend/Controller/Action.php';

class contacts_IndexController extends Cms_Controller_Action
{
	public function init()
	{
		$this->view->siteTitle = 'Contatcts';
		$this->currentPage = 1;
		$this->resultsPerPage = 12;
		$this->view->userPhotos = $this->CONFIG->path->usersThumb;
		$this->view->userImage = $this->CONFIG->path->usersThumb;
		$this->userId = $_SESSION["sess_userid"];
		$group_details = Doctrine::getTable('Groups')->getGroupsById($this->userId,1,1);
		//echo '<pre>';print_r(count($group_details));exit;
		$this->view->group_details= $group_details;
		if($group_details)
		{
		$this->view->count= count($group_details);
		}else
		{
		$this->view->count= 0;
		}
			
	}
	public function indexAction()
	{

	  //echo 'TEST';exit;
		$memid=$_SESSION["sess_userid"];
		$this->view->params ="";
		$getContatcts = Doctrine::getTable('Contacts')->getAllContacts($memid,$this->currentPage,$this->resultsPerPage,'');
		//echo '<pre>';print_r($getContatcts);exit;
		$this->view->getContatcts = $getContatcts;
	}
	public function paginationAction()
	{
		$memid=$_SESSION["sess_userid"];
		$this->view->params ="";

		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 12:$this->_getParam('results_per_page');
		$serValue = $this->_getParam('orderby') == ''? '':$this->_getParam('orderby');
		//echo $serValue;
		if($serValue == '0' || $serValue=='')
		{
			//echo 'ddddddddd';exit;
			$serValue = '';
		} else {
			$serValue = $serValue;
		}
		//echo $serValue;exit;
		$getContatcts = Doctrine::getTable('Contacts')->getAllContacts($memid,$currentPage,$resultsPerPage,$serValue);
		/* Colors */
		$this->view->getContatcts = $getContatcts;
		$jsonData['output'] = $this->view->render('index/friends-list.html');
		$jsonData['pages'] = $getContatcts['pages'];
	    $jsonData['pager'] = $getContatcts['pager'];
	    $jsonData['currentPage'] = $getContatcts['currentPage'];
		$jsonData['resultsCount'] = $getContatcts['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
		
	}
	public function addContactAction()
	{
		//$this->_helper->layout->disableLayout();
	}
	public function sendSearchAction()
	{
		$searchTearm = trim($_POST['sindividual'],'');
		$getUserConnectionsArray = Doctrine::getTable('Contacts')->getMyConnectionsArray($this->userId);
		$this->view->userConnectionsArray = $getUserConnectionsArray;
		$this->view->sindividual = $_POST['sindividual'];
		$searchResult = Doctrine::getTable('Users')->getUsersBySearch($searchTearm,0, $this->resultsPerPage);
		$this->view->searchResults = $searchResult ;
		$jsonData['output'] = $this->view->render('index/search-results.html');
		$jsonData['pages'] = $searchResult['pages'];
	    $jsonData['pager'] = $searchResult['pager'];
	    $jsonData['currentPage'] = $searchResult['currentPage'];
		$jsonData['resultsCount'] = $searchResult['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
		
	}
	public function searchPaginationAction()
	{
		$currentPage = $this->_getParam('current_page') == 0? 1:$this->_getParam('current_page');
		$searchTearm = $_GET['sindividual'];
		$resultsPerPage = $this->_getParam('results_per_page') == 0? 5:$this->_getParam('results_per_page');
		$getUserConnectionsArray = Doctrine::getTable('Contacts')->getMyConnectionsArray($this->userId);
		$this->view->userConnectionsArray = $getUserConnectionsArray;
		$this->view->sindividual = $_GET['sindividual'];
		$searchResult = Doctrine::getTable('Users')->getUsersBySearch($searchTearm,$currentPage, $this->resultsPerPage);
		$this->view->searchResults = $searchResult ;
		$jsonData['output'] = $this->view->render('index/search-results.html');
		$jsonData['pages'] = $searchResult['pages'];
	    $jsonData['pager'] = $searchResult['pager'];
	    $jsonData['currentPage'] = $searchResult['currentPage'];
		$jsonData['resultsCount'] = $searchResult['resultsCount'];
		$this->_helper->json->sendJson($jsonData);
	}
	public function saveContactAction()
	{
		//echo $this->userId;exit;
		$_POST['list'] = trim($_POST['list'],',');
		$ids = explode(',',$_POST['list']);
		//echo '<pre>';print_r($ids);exit;
		foreach($ids as $id)
		{
			$contactId = Doctrine::getTable('Contacts')->saveContact($this->userId,$id);
		}
		$jsonData['contactId'] = $contactId;
		$this->_helper->json->sendJson($jsonData);	
	}
	public function deleteContactAction()
	{
		//echo $this->userId;exit;
		$_POST['list'] = trim($_POST['list'],',');
		$ids = explode(',',$_POST['list']);
		//echo '<pre>';print_r($ids);exit;
		foreach($ids as $id)
		{
			$contactId = Doctrine::getTable('Contacts')->deleteContact($id);
		}
		$jsonData['contactId'] = $contactId;
		$this->_helper->json->sendJson($jsonData);	
	}
	public function groupviewAction()
	{
		$group_id=base64_decode($this->_getParam('id'));
		//echo $group_id;exit;
		$memid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		$groupName = Doctrine::getTable('Groups')->getGroupDetailsUsingId($group_id);
		$result = Doctrine::getTable('GroupDetails')->getGroupDetailsByGroupId($group_id);
		//echo '<pre>';print_r($groupName);exit;
		$count=count($result);
		$this->view->count=$count;
		$this->view->gname=$groupName[0]['name'];
		$this->view->group_id=$group_id;
		$this->view->result=$result;	
	}
	public function groupsAction()
	{
		//echo $_POST;exit;
		$this->view->type= 7;
		$message=$this->_getParam('current_page');
		//echo $message;exit;
			if($message == 'success')
			{
			$this->view->message="Group added successfully.";
			}
			else if($message == 'fail')
			{
				$this->view->message="Group Already Exist/fail";
			} else {
				$this->view->message="";
			}
		$memid = $_SESSION["sess_userid"];
		//echo $memid;exit;
		$group_type="Triggers";
		$result = Doctrine::getTable('Groups')->getGroupDetails($memid,$group_type,$this->currentPage,$this->resultsPerPage);
		//echo '<pre>';print_r($result);exit;
		$count=count($result['result']);
		$this->view->total=$count;
		$this->view->result=$result;

	}
}	
	